<?php

namespace platform\models;

use common\services\Authorization;
use platform\core\BaseModel;
use cm;
use Yii;
use yii\helpers\Url;

class CarModel extends BaseModel
{
    /*
    * 车型管理
    */
    public function actionIndex()
    {
        if (Cm::$req->isPost()) {
            $action = Cm::$req->get('action');
            switch ($action) {
                case "list":
                    $key = Cm::$req->post('key', ['null', 'empty', 'default' => '']);
                    $models_id = (int)Cm::$req->post('models_id', ['null', 'empty']);
                    $pageIndex = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
                    $pageCount = Cm::$req->post('limit', ['null', 'empty', 'default' => 20]);
                    $where = " 1=1 AND c.status=1 ";
                    if(!empty($models_id)){
                        $where .= " AND c.models_id={$models_id}";
                    }
                    if(!empty($key)){
                        $where .= " AND (c.plate LIKE '%{$key}%' OR c.driver LIKE '%{$key}%')";
                    }
                    $data = Cm::$db->paging("SELECT c.*,cm.name
                            FROM car c 
                            LEFT JOIN car_models cm ON cm.id=c.models_id
                            WHERE {$where}  ORDER BY  c.id desc ",$pageIndex, $pageCount);
                    Cm::$res->paging($data);
                    break;
                case 'save':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    $regplate = '/^([\x{4e00}-\x{9fa5}]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\x{4e00}-\x{9fa5}]|[W]{1}[A-Z]{1}[\x{4e00}-\x{9fa5}]{1}[0-9]{4}[0-9A-Z]{1}|[\x{4e00}-\x{9fa5}]{1}[a-zA-Z]{1}[DF]{1}[a-zA-Z_0-9]{1}[0-9]{4}|[\x{4e00}-\x{9fa5}]{1}[a-zA-Z]{1}[0-9]{5}[DF]{1}|[a-zA-Z]{2}[0-9]{5}|[\x{4e00}-\x{9fa5}]{1}[a-zA-Z]{1}[DF]{1}[a-zA-Z_0-9]{5})$/u';
                    $mdata = [
                        'plate' => trim(strtoupper(Cm::$req->post('plate',['title'=>'车牌号', 'reg' => $regplate]))),
                        'driver' => trim(Cm::$req->post('driver',['title'=>'司机名称'])),
                        'telephone' => Cm::$req->post('telephone', ['null', 'empty', 'default' => '']),
                        'models_id' => Cm::$req->post('models_id', ['null', 'empty', 'default' => 0]),
                        'c_seat' => (int)Cm::$req->post('c_seat', ['null', 'empty', 'default' => 0]),
                        'p_seat' => (int)Cm::$req->post('p_seat', ['null', 'empty', 'default' => 0]),
                        'updatetime' => date("Y-m-d H:i:s",time())
                    ];
                    if(empty($mdata['models_id'])){
                        Cm::$res->error('保存失败,请选择车型');
                    }
                    if(empty($mdata['c_seat']) || empty($mdata['p_seat'])){
                        Cm::$res->error('保存失败,请填写座位数');
                    }
                    if($mdata['c_seat']<$mdata['p_seat']){
                        Cm::$res->error('保存失败,核定座位数不能小于乘客座位数');
                    }
                    $hasplate = Cm::$db->one("SELECT id FROM car WHERE plate='{$mdata['plate']}' AND id!={$id} AND status=1");
                    if(!empty($hasplate)){
                        Cm::$res->error('保存失败,该车牌号已存在');    
                    }
                    $mdata['telephone'] = trim($mdata['telephone']);
                    if(!empty($id)){
                        Cm::$db->update('car', $mdata, "id={$id}");
                        Cm::$app->log->authlog(21,$mdata,$id);
                    }else{
                        $mdata['addtime'] = date('Y-m-d H:i:s');
                        $mdata['status'] = 1;
                        $id = Cm::$db->insert('car', $mdata);
                        if(!$id){
                            Cm::$res->error('保存失败');
                        }
                        Cm::$app->log->authlog(20,$mdata,$id);
                    }
                    Cm::$res->susess([], '保存成功');
                    break;      
                case 'del':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    if(empty($id)){
                        Cm::$res->error('请选择删除的车辆');
                    }
                    $mdata = ['status' => 2,'updatetime' => date("Y-m-d H:i:s",time())];
                    Cm::$db->update("car", $mdata, "id={$id}");
                    Cm::$app->log->authlog(19,$mdata,$id);
                    Cm::$res->susess();
                    break; 
            }
        }
        
        $data['map'] = [];
        $data['map'][] = ['title' => "车辆管理", 'url' => Url::to(['car/index'])];
        $data['baseUrl'] = Url::to(['car/index']);
        $data['carModels'] = $this->carModels();
        return $data;
    }

    /*
    * 车型管理
    */
    public function actionModel()
    {
        if (Cm::$req->isPost()) {
            $action = Cm::$req->get('action');
            switch ($action) {
                case "list":
                    $key = Cm::$req->post('key', ['null', 'empty', 'default' => '']);
                    $seat = (int)Cm::$req->post('seat', ['null', 'empty']);
                    $type_id = (int)Cm::$req->post('type_id', ['null', 'empty']);
                    $pageIndex = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
                    $pageCount = Cm::$req->post('limit', ['null', 'empty', 'default' => 20]);
                    $where = " 1=1 AND cm.status=1 ";
                    if(!empty($type_id)){
                        $where .= " AND cm.type_id={$type_id}";
                    }
                    if(!empty($seat)){
                        $where .= " AND cm.seat_type_id={$seat}";
                    }
                    if(!empty($key)){
                        $where .= " AND cm.name LIKE '%{$key}%'";
                    }
                    $data = Cm::$db->paging("SELECT cm.*,cmt.name AS typestr,cst.name AS seatstr 
                            FROM car_models cm 
                            LEFT JOIN car_models_type cmt ON cmt.id=cm.type_id
                            LEFT JOIN car_seat_type cst ON cst.id=cm.seat_type_id
                            WHERE {$where}",$pageIndex, $pageCount);
                    Cm::$res->paging($data);
                    break;
                case 'save':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    $mdata = [
                        'name' => trim(Cm::$req->post('name',['title'=>'车型名称'])),
                        'serial_number' => trim(Cm::$req->post('serial_number',['title'=>'编码'])),
                        'type_id' => Cm::$req->post('type_id', ['null', 'empty', 'default' => 0]),
                        'seat_type_id' => Cm::$req->post('seat_type_id', ['null', 'empty', 'default' => 0]),
                        'c_seat' => (int)Cm::$req->post('c_seat', ['null', 'empty', 'default' => 0]),
                        'p_seat' => (int)Cm::$req->post('p_seat', ['null', 'empty', 'default' => 0]),
                        'updatetime' => date("Y-m-d H:i:s",time())
                    ];
                    if(empty($mdata['type_id'])){
                        Cm::$res->error('保存失败,请选择车辆类型');
                    }
                    if(empty($mdata['seat_type_id'])){
                        Cm::$res->error('保存失败,请选择座位类型');    
                    }
                    if(empty($mdata['c_seat']) || empty($mdata['p_seat'])){
                        Cm::$res->error('保存失败,请填写座位数');
                    }
                    if($mdata['c_seat']<$mdata['p_seat']){
                        Cm::$res->error('保存失败,核定座位数不能小于乘客座位数');
                    }

                    if(!empty($id)){
                        Cm::$db->update('car_models', $mdata, "id={$id}");
                        Cm::$app->log->authlog(24,$mdata,$id);
                    }else{
                        $mdata['addtime'] = date('Y-m-d H:i:s');
                        $mdata['status'] = 1;
                        $id = Cm::$db->insert('car_models', $mdata);
                        if(!$id){
                            Cm::$res->error('保存失败');
                        }
                        Cm::$app->log->authlog(23,$mdata,$id);
                    }
                    Cm::$res->susess([], '保存成功');
                    break;      
                case 'del':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    if(empty($id)){
                        Cm::$res->error('请选择删除的车型');
                    }
                    $mdata = ['status' => 2,'updatetime' => date("Y-m-d H:i:s",time())];
                    Cm::$db->update("car_models", $mdata, "id={$id}");
                    Cm::$app->log->authlog(22,$mdata,$id);
                    Cm::$res->susess();
                    break; 
            }
        }
        
        $data['map'] = [];
        $data['map'][] = ['title' => "车辆管理", 'url' => Url::to(['car/index'])];
        $data['map'][] = ['title' => "车型管理", 'url' => Url::to(['car/model'])];
        $data['baseUrl'] = Url::to(['car/model']);
        $data['carModelsType'] = $this->carModelsType();
        $data['carSeatType'] = $this->carSeatType();
        return $data;
    }
}